package com.hualan.sfzy.controller;

import com.hualan.sfzy.constant.CommonConstant;
import com.hualan.sfzy.model.entity.Consult;
import com.hualan.sfzy.model.entity.Course;
import com.hualan.sfzy.service.CourseService;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @PostMapping
    public Result addCourse(@RequestBody Course course) {
        int i=courseService.add(course);
        if(i>0){
            return Result.success();
        }
            return Result.error("添加失败");
    }
    @PutMapping
    public Result updateCourse(@RequestBody Course course) {
        int i=courseService.edit(course);
        if(i>0){
            return Result.success();
        }
        return Result.error("修改失败");
    }
    @DeleteMapping("{id}")
    public Result deleteCourse(@PathVariable int id) {
        System.out.println(id);
        int i=courseService.remove(id);
        if(i>0){
            return Result.success();
        }
        return Result.error("删除失败");
    }
    @DeleteMapping()
    public Result deleteCourses(@RequestBody Integer[] ids) {
        int i=courseService.removeBatch(ids);
        if(i>0){
            return Result.success();
        }
        return Result.error("删除失败");

    }
    @GetMapping("/all")
    public Result getAllCourse(@RequestParam (value ="pageNum" ,defaultValue = CommonConstant.PAGE_NUM_DEFAULT)Integer pageNum,
                               @RequestParam (value = "pageSize" ,defaultValue = CommonConstant.PAGE_SIZE_DEFAULT)Integer pageSize,
                               Course course) {
        Map<String,Object> map=courseService.findPage(pageNum,pageSize,course);

            return Result.success(map);

    }
    @GetMapping()
    public Result getCourseById(@RequestParam int id) {
        Course course=courseService.findById(id);

            return Result.success(course);

    }
    @GetMapping("/psychologist")
    public Result getPsychologist() {
        List<Consult> list=courseService.getAllPsychologist();
        return Result.success(list);
    }


}
